<?php
Yii::import('dao.share.class.Invited');
YII::import('application.extensions.YiiMailer.YiiMailer');
class InviteEmail extends Invited{
    public $emailer;
    public $email;
    
    public function init(){
        return parent::init();
    }
    
    public function rules(){
        return array_merge(parent::rules(),array(
               array('customerId','isLogin','on'=>'generate'),
               array('email','email','on'=>'generate'),
               array('email','hasRegistered','on'=>'generate'),
               array('email','beInvited','on'=>'generate'),
               array('refer','hasRefer','on'=>'register'),
        ));
    }
    
    public function isLogin($attribute,$params){
        if (Yii::app()->user->isGuest){
            $this->addError($attribute, 'You should login!');
        }
    }
    
    public function hasRegistered($attribute,$params){
         $customer=new Customer();  
         $customer=$customer->findByAttributes(array('email'=>$this->email));
         if (!empty($customer)){
             $this->addError($attribute, 'This Email is registered!');
         }
    }
    
    public function beInvited($attribute,$params){
        $invite=new Invite();
        $invite=$invite->findByAttributes(array(
            'customer_id'=>$this->customerId,
            'email'=>$this->email
        ));
        if (!empty($invite)){
            $this->addError($attribute, 'This Email you have invited!');
        }
    }
    
    public function hasRefer($attribute,$params){
        $this->refer=$this->getCookie();
        $refer=new Invite();
        $refer=$refer->findByAttributes(array(
            'email'       =>$this->email,
            'refer'        =>$this->refer,
            'status'      =>self::INVITE_GENERATE
        ));
        if (empty($refer)){
            $this->addError($attribute, "ref is not valid!");
        }
    }
    
    public function addSuccess($customerId){
        $invite=new InviteSuccess();
        $invite->attributes=array(
            'customer_id'=>$customerId,
            'email'=>$this->email,
            'created'=>time(),
            'refer'=>$this->refer,
        );
        return $invite->save();
    }
    
    public function changeStatus($customerId){
        $invite=new Invite();
        $invite=$invite->findByAttributes(array(
            'customer_id' =>$customerId,
            'email'            =>$this->email,
            'status'           =>self::INVITE_GENERATE 
        ));
        if (!empty($invite)){
            $invite->status=self::INVITE_SUCCESS;
            $invite->save();
        }
    }
    
    public function getReferals(){
        $Referals=new Invite();
        $Referals=$Referals->findAllByAttributes(array(
            'refer'          =>$this->refer,
            'email'      =>$this->email,
            'status'     =>self::INVITE_GENERATE,
        ));
        $Result=null;
        if (!empty($Referals)){
            $Result=$Referals;
        }
        return $Result;
    }
    
    public function add(){
        $invite=new Invite();
        $invite->attributes=array(
            'customer_id'=>$this->customerId,
            'email'=>$this->email,
            'created'=>time(),
            'refer'=>$this->refer,
            'status'=>self::INVITE_GENERATE,
        );  
        $result=$invite->save();
        if (!$result){
            $this->addError("save", $invite->getErrors());
        }
    }
    
    public function send(){
        $mailer = new YiiMailer();
        $mailer->setView('referral');
        $customer=Customer::model()->findByPk($this->customerId);
        $username=$customer->user_name;
        $mailer->setData(array(
            'friend_name'       =>$this->email,
            'friend_message'  =>'',
            'username'           =>$username,
            'url'                       =>$this->url
        ));
        $mailer->AddAddress($this->email);
        $mailer->Subject = Yii::t('demo', $username.' wants you to join eGunner Penny Auctions');
        return $mailer->send();
    }
    
    public function generate(){
        $this->generateUrl();
        $this->getRef();
        $this->add();
        return $this->send();
    }
    
    public function visit(){
        $this->setCookie('refer', $this->refer);
    }
    
    
    public function success(){
        $Referals=$this->getReferals();
        if ($Referals!==null){
            foreach ($Referals as $Referal){
                $this->changeStatus($Referal->customer_id);
                $this->addSuccess($Referal->customer_id);
                $this->_bidDao->customerId=$Referal->customer_id;
                $bidModel=$this->_bidDao->getBidModel('promo');
                $bidModel->setBid(self::INVITE_SUCCESS_BONUS,"INVITE",'');
            }
        }
    }
}